local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local base = require "base"
local tabletool = require "tabletool"
local timetool = require "timetool"
require "gang28enum"

local filename = "tabletimer.lua"

local TableTimer = {}

function TableTimer.doready(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]	
	if seat.rid ~= request.rid then
		return
	end
	if timerid ~= seat.ready_timer_id then
		return
	end
	seat.ready_timer_id = -1
	seat.ready_to_time = 0
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		-----判断朋友桌局数到没有
		if seat ~= nil then
			if table_data.cur_games_times < table_data.conf.game_times then
				seat.state = ESeatState.SEAT_STATE_WAIT_START
				server.logic.tablelogic.gameready(table_data, request, seat)
				return
			end
			server.logic.tablelogic.passive_standuptable(table_data, seat)
		end
	else
		if seat.rid > 0 then
			server.logic.tablelogic.passive_standuptable(table_data, seat)
		end
	end
end

function TableTimer.delete_table(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.delete_table_timer_id == timerid then
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
				server.logic.tablelogic.passive_standuptable(table_data, seat)
			end
		end
		table_data.delete_table_timer_id = -1
        server.delete()
    end
end

function TableTimer.callover(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.timer_id == timerid then
		table_data.timer_id = -1
		table_data.waittotime = nil
		table_data.state = ETableState.TABLE_STATE_WAIT
		server.logic.gamelogic.setbanker()
	end
end

function TableTimer.betover(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.timer_id == timerid then
		table_data.timer_id = -1
		table_data.waittotime = nil
		table_data.state = ETableState.TABLE_STATE_WAIT
		for _, seat in ipairs(table_data.seats) do
			if seat.state == ESeatState.SEAT_STATE_PLAYING and seat.index ~= table_data.banker then
				if seat.bets == 0 then
					seat.bets = 1
					msghelper:sendmsg_to_alltableplayer("playerbetresultN", {
						rid = seat.rid,
						seat_index = seat.index,
						value = 1,
					})
				end
			end
		end
		if table_data.conf.game_type == EGameType.GAME_TYPE_FREE then
			server.logic.gamelogic.sendcards(2)
		elseif table_data.conf.game_type == EGameType.GAME_TYPE_SEECARD then
			server.logic.gamelogic.sendcards(1)
		end
		table_data.timer_id = timer.settimer(5 * 100, "checkcards")
		table_data.waittotime = timetool.get_time() + 5
		msghelper:sendmsg_to_alltableplayer("checkcardsN", {checktotime = table_data.waittotime})
	end
end

function TableTimer.checkcards(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.timer_id == timerid then
		table_data.timer_id = -1
		table_data.waittotime = nil
		server.logic.gamelogic.checkcards()
	end
end

function TableTimer.gameover(timerid)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.timer_id == timerid then
		table_data.timer_id = -1
		server.logic.gamelogic.gameover()
	end
end

return TableTimer